from django.http import HttpResponseRedirect
from django.shortcuts import render, HttpResponse

# Create your views here.
from common.forms import *



def menu_list(request):
    menuString=Menu.getMenuString()
    menus = Menu.objects.all().order_by('orderNumber')
    request.session['menuString']=menuString
    request.session['a']="a"

    return render(request, "common/menu_list.html", locals())


def menu_edit(request, id=0):
    id=int(id)
    if id != 0:
        menu = Menu.objects.get(id=id)
    else:
        menu = None
    if request.method == 'POST':
        form = MenuForm(request.POST, instance=menu)
        if form.is_valid():
            menu = form.save()
            Menu.updateOrder()
            return HttpResponseRedirect('/common/menu_list')
        else:
            return render(request, 'common/menu_edit.html', locals())
    else:
        form = MenuForm(instance=menu)
    return render(request, 'common/menu_edit.html', locals())


def menu_delete(request, id=0):
    Menu.objects.get(id=id).delete()
    return HttpResponseRedirect('/common/menu_list')



